#!/usr/bin/python
"""
Author:			Jimmy Saw
Date modified:	12/25/2011

Description:    This script combines 2 Fastq files (paired end) into 1 and removes
                trailing 'B's that represent low quality sequences.

Usage:          python dissertation_CombineFastq2BtrimmedFastaQual.py <Fastq1> <Fastq2> <new Fastq>

Example:
dissertation_CombineFastq2BtrimmedFastaQual.py file1.fastq file2.fastq new.fastq
"""

import sys
from Bio.SeqIO.QualityIO import FastqGeneralIterator

fastq1 = sys.argv[1]
fastq2 = sys.argv[2]
outfile = sys.argv[3]

handle = open(outfile, "w")
min_length = 10
for title, seq, qual in FastqGeneralIterator(open(fastq1)):
#    qual = qual.rstrip("B") #Remove any trailing B characters
    length = len(qual)
    if length >= min_length:
        seq = seq[:length] #trim to match
        handle.write("@%s\n%s\n+\n%s\n" % (title, seq, qual))
handle.close()
